package com.sdry.web.controller.y;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.Car;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.lz.WarehouseRegion;
import com.sdry.model.y.Transport;
import com.sdry.model.zc.ZcGeneralQueryEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.lz.InventoryService;
import com.sdry.service.lz.StockOutOrderService;
import com.sdry.service.y.TransportService;
import com.sdry.utils.Page;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/transport")
public class TransportController {
	public static final Integer AFFECT_ROW = 0;

    @Resource
    TransportService   transportService;
    @Resource
	InventoryService inventoryService;
    
    @Resource 
	StockOutOrderService stockOutOrderService;
  //跳转路径
  	@RequestMapping("/transport")
  	public String transport(Model model){
  		
  		List<Warehouse> warehouseList = inventoryService.queryAllWarehouseAndroid();
  		model.addAttribute("warehouseList", warehouseList);
  		List<Car> carList = stockOutOrderService.queryAllCar();
  		model.addAttribute("allCar", carList);
  		return "/y/transport";
  	}
    /**
             *    新增
    */
    @RequestMapping("/addTransport")
    public void addTransport(HttpServletResponse response, Transport transport,HttpSession session){
    	ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
    	transport.setCreater(staf.getId());
    	Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		transport.setCtime(dateFormat.format(now));
        Long affectRow = transportService.addTransport(transport);
        try {
            ResponseUtil.write(response, affectRow);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
            * 根据ID删除该行数据
     * @param response
     * @param idArr
     */
    @RequestMapping("/deleteTransport")
    public void deleteTransport(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
        if(idArr.length > 0) {
            for (int i = 0; i < idArr.length; i++) {
                Long affectRow =transportService.deleteTransport(Long.parseLong(idArr[i]));
                if (affectRow > AFFECT_ROW) {
                    try {
                        ResponseUtil.write(response, affectRow);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        ResponseUtil.write(response, AFFECT_ROW);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
         * 行内编辑
     * @param response
     */
    @RequestMapping("/updateTransport")
    public void updateTransport(HttpServletResponse response, Transport transport) {
        Long affectRow = transportService.updateTransport(transport);
        if(affectRow > AFFECT_ROW){
            try {
                ResponseUtil.write(response, affectRow);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                ResponseUtil.write(response, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
              * 分页查询
    
     */
    @RequestMapping("/findTransport")
    public void findTransport(HttpServletResponse response, Page page, String carid, String date){
    	Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		
		if(carid!=null&& !"".equals(carid)) {
			map.put("carid", carid);
			
		}else {
			map.put("carid", "");
			
			}
		if(date!=null&& !"".equals(date)) {
			map.put("date", date);
			
		}else {
			map.put("date", "");
			
			}
		List<Transport> transportList = transportService.findTransport(map);
        Long count = transportService.countTransport(map);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(transportList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}
